package Records.Connector;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alexander
 */
public class CheckConnection {

    private boolean Connected;

    /**
     * @return the Connected
     */
    public boolean isConnected() {
        return Connected;
    }

    public CheckConnection(String http){
        try {
            checkIsConnected(new URL(http));
        } catch (MalformedURLException ex) {
            Logger.getLogger(CheckConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * @param isConnected the isConnected to set
     * @return
     */
    
    public boolean checkIsConnected(URL url) {
        System.out.println("Internet_Check.checkIsConnected(" + url.toString() + ")");

        try {
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.connect();
            if (con.getResponseCode() == 200) {
                System.out.println("Connection established!!");
                Connected = true;
            }
        } catch (Exception ex) {
            System.out.println("No Connection");
            Connected = false;
        }
        return isConnected();
    }
}
